#!/bin/bash
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

FILEPATH="/var/hsc/log"
FILELIST='FNM_Comm.log FNM_Init.log FNM_Recov.log FNM_Route.log fnmhw.cfg topology.map SNM_RPM iqyylog.log iqzdtrac.trm'
startFNMD_TRACE=/var/hsc/log/startFNMD.log
exec 3> $startFNMD_TRACE

USAGE="Usage: startFNMD [ -h | -V ]"

HFLAG=0
VFLAG=0

# TODO this test should be for number of parms == 1!
if (( $# == 1 ))
then
    while test -n "$1"
        do
            case $1 in
                -h) HFLAG=1; break;;
                -V) VFLAG=1; break;;
                 *) echo $USAGE >> $startFNMD_TRACE; exit 2;;
            esac
        done
elif (( $# > 1 )) 
then
	echo $USAGE >> $startFNMD_TRACE
	exit 2	
fi

# Start the fnmd limit core files to 1,000,000 KB (1GB max!)
# count is in blocks
ulimit -c 1000000

# set the library paths for libACE.so vpd.lib devDB.lib
unset LD_ASSUME_KERNEL
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/hsc/lib
# 
# The variable below changes the number of logs saved.  
# Activating the line with a postive number (ex 5) will
# save *.log.1 through *.log.5 where *.log.5 is the oldest log.
# This applies to all component logs in /var/hsc/log. 
export FNM_LOG_SAVE_COUNT=15

# sleep 1 min to allow hwdr_svr vports to stablize
sleep 60 
echo "VFLAG=$VFLAG" >> $startFNMD_TRACE
echo "HFLAG=$HFLAG" >> $startFNMD_TRACE

if [ -f /var/hsc/log/fnmd.log ]; then
        FNMDLOGSIZE=$( du -k /var/hsc/log/fnmd.log | tail -n 1 | awk '{print $1}' )

        if [ $FNMDLOGSIZE -gt 10 ]; then
                mv /var/hsc/log/fnmd.log /var/hsc/log/fnmd.log.old
        fi
fi

# start and get the main thread's PID work
if [ $HFLAG -eq 1 ]; then
   /opt/hsc/bin/fnmd -h >> /var/hsc/log/fnmd.log 2>&1 & 
   TOP_FNMD_ID=$!
elif [ $VFLAG -eq 1 ]; then
   /opt/hsc/bin/fnmd -V >> /var/hsc/log/fnmd.log 2>&1 &
   TOP_FNMD_ID=$!
else 
   /opt/hsc/bin/fnmd >> /var/hsc/log/fnmd.log 2>&1 &
   TOP_FNMD_ID=$!
fi

# Record the thread's PID for the started fnmd
# this is Linux specific 7.2 will record all threads currently running
sleep 10
ps -ef | grep fnmd | grep opt | awk {'print $2'} > /var/hsc/fnmd_proc_ids 

# wait for the main thread to exit
wait $TOP_FNMD_ID
echo "fnmd stopped on "`date` > /tmp/fnmdStopped.log
echo "fnmd stopped on "`date`

# run snap tool to capture data after exit
/opt/hsc/bin/fnm.snap

# remove old logs after exit
for filename in $FILELIST
do
    if [ -f ${FILEPATH}/$filename ]; then
        fileList=$fileList" "$filename
    fi
done
  
cd ${FILEPATH}
for filename in $(ls FNM_Route.log.old*);
do  
        fileList=$fileList" "$filename
done

for filename in $(ls FNM_Comm.log.old*);
do
        fileList=$fileList" "$filename
done

for filename in $(ls FNM_Init.log.old*);
do
        fileList=$fileList" "$filename
done
  
for filename in $(ls FNM_Recov.log.old*);
do
        fileList=$fileList" "$filename
done

echo ================================================================ >> $startFNMD_TRACE
date                                                                  >> $startFNMD_TRACE
echo fnm.snap running on `hostname`                                   >> $startFNMD_TRACE
echo ================================================================ >> $startFNMD_TRACE

# check to make sure fnm.snap ran properly
value=""
filesize=0

while read line
do
  value=$line;
done < "fnm.snap.log"

check=$(echo $value | cut -d/ -f5)
check=$check".gz"
filesize=`ls -ld ${FILEPATH}/$check | cut -c "31-42"`

if [ $filesize > 0 ]; then
	# remove files
	echo Removing Files: $fileList                                        >> $startFNMD_TRACE
	for filename in $fileList;
	do
		rm "$filename"
	done
fi
